package SE2;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.GregorianCalendar;

import javax.swing.JOptionPane;

/**
 *
 * @author Max
 */
public class LoginFrame extends javax.swing.JFrame
{

    /** Creates new form LoginFrame */
    public LoginFrame()
    {
        initComponents(); 
        this.jTextField1.setText("Wielsch");
        this.jPasswordField1.setText("12345");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        this.setTitle("Lagerverwaltung Automobil AG");
        
        jTextField1.setText("<familyname>");

        jLabel1.setText("Login");

        jLabel2.setText("Username:");

        jLabel3.setText("Password:");

        jButton1.setText("Anmelden");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton1)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(8, 8, 8))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addGap(10, 10, 10)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                                .addComponent(jPasswordField1)))))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    @SuppressWarnings("deprecation")
	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {//GEN-FIRST:event_jButton1ActionPerformed
    	if (checkPassword().equals("scheduler"))
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {

				public void run()
                {
                    new SchedulerFrame
                    (
                    	new ProductionScheduler
                    	(
                    		LoginFrame.this.jTextField1.getText(),
                    		LoginFrame.this.jPasswordField1.getText(),
                    		new GregorianCalendar()
                    	)
                    ).setVisible(true);
                }});
            this.dispose();
        }
    	else if (checkPassword().equals("purchaser"))
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                @SuppressWarnings("deprecation")
				public void run()
                {
                    new PurchaserFrame
                    (
                    	new Purchaser
                    	(
    						LoginFrame.this.jTextField1.getText(),
    						LoginFrame.this.jPasswordField1.getText(),
    						new GregorianCalendar()
    					)
                    ).setVisible(true);
                }
            });
            this.dispose();
        }
        else if (checkPassword().equals("No Permission!"))
        {
            JOptionPane.showMessageDialog(null, "Sie haben keine Befugnis das System zu benutzen!");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    public String checkPassword()
    {
    	String user = this.jTextField1.getText();
    	String password = this.jPasswordField1.getText();
    	
        DatabaseConnection db = new DatabaseConnection();
        ResultSet res = db.executeStatement("SELECT * FROM employee WHERE familyname = '" + user + "' AND password = '" + password + "';");
        try
        {
			while (res.next())
			{
				return res.getString("function");
			}
		}
        catch (SQLException e)
        {
			e.printStackTrace();
		}
        return "No Permission!";
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
